//引入仓库模型
const { packModule } = require('../modules/packModule');
//添加
async function addPack(req, res, next) {
    const data = req.body;
    const result = await packModule.create(data);
    if (result) {
        res.send({
            message: "新增仓库成功",
            code: 200
        })
    } else {
        res.send({
            message: "新增仓库成功",
            code: 500
        })
    }
}
//获取
async function getPack(req, res, next) {
    const { value, currentPage, pageSize } = req.query;
    //获取数据库总数据
    const total = await packModule.countDocuments({
        $or: [
            { packName: { $regex: value, $options: '$i' } },
            { packHome: { $regex: value, $options: '$i' } },
            { packMan: { $regex: value, $options: '$i' } },
            { packTel: { $regex: value, $options: '$i' } },
            { packEmail: { $regex: value, $options: '$i' } },
            { packAddress: { $regex: value, $options: '$i' } },
        ]
    });
    //换算成对应的页数
    const pages = Math.ceil(total / pageSize);


    const result = await packModule
        .find({
            $or: [
                { packName: { $regex: value, $options: '$i' } },
                { packHome: { $regex: value, $options: '$i' } },
                { packMan: { $regex: value, $options: '$i' } },
                { packTel: { $regex: value, $options: '$i' } },
                { packEmail: { $regex: value, $options: '$i' } },
                { packAddress: { $regex: value, $options: '$i' } },
            ]
        })
        .limit(pageSize - 0)
        .skip((currentPage - 1) * pageSize);

    if (result.length > 0) {
        res.send({
            message: "获取成功",
            code: 200,
            result,
            total,
            pages
        })
    }

}
//查询
async function findPack(req, res, next) {
    const _id = req.params;
    const result = await packModule.find(_id);
    if (result.length > 0) {
        res.send({
            message: "查询成功",
            code: 200,
            result
        })
    }
}
//修改仓库
async function changePack(req, res, next) {
    const data = req.body;
    console.log("前端传输的数据",data);
    const result = await packModule.updateOne({ _id: data._id }, data);
    console.log('数据库返回的数据',result);
    if (result.nModified > 0) {
        res.send({
            message: "修改成功",
            code: 200
        })
    } else {
        res.send({
            message: "修改失败",
            code: 500
        })
    }
}
//删除仓库
async function delPack(req, res, next) {
    const data = req.body;
    const result = await packModule.deleteOne(data);
    if (result.deletedCount > 0) {
        res.send({
            message: "删除成功",
            code: 200
        })
    } else {
        res.send({
            message: "删除失败",
            code: 500
        })
    }
}
//切换仓库
async function startPack(req, res, next) {
    const data = req.query;
    const result = await packModule.updateOne({ _id: data._id }, { packState: data.packState });
    console.log("数据库传输的数据", result);
    if (result.nModified > 0) {
        res.send({
            message: "启用成功",
            code: 200
        })
    } else {
        res.sed({
            message: "启用失败",
            code: 500
        })
    }
}
module.exports = {
    addPack, getPack, findPack, changePack, delPack, startPack
}